﻿using Ops.Mpc.States;
using Ops.Mpc.ExampleApplication.DataTemplateBindings.Models;
using System.Windows;
using Ops.Mpc.ExampleApplication.ExampleService;
using System.Threading;


namespace Ops.Mpc.ExampleApplication.DataTemplateBindings.Actions
{
    public class SelectedPersonChangedStateAction : StateAction
    {
        Timer _timer = null;
        DataTemplateModel dataTemplateModel = null;

        public override void Start()
        {
            base.Start();
            dataTemplateModel  = this.GetModel<DataTemplateModel>();
        }

        protected override void OnExecute(object context)
        {
            base.OnExecute(context);
            
            
            _timer = new Timer((timerContext) =>
                {
                    SelectedPersonChangedStateAction selectedPersonChangedStateAction = timerContext as SelectedPersonChangedStateAction;

                    dataTemplateModel.SelectedPerson = context as Person;

                    selectedPersonChangedStateAction._timer.Dispose();
                    selectedPersonChangedStateAction._timer = null;

                    this.Finish();

                },this,2000,Timeout.Infinite);
 
        }




    }
}
